#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 8000000UL							// 8 MHz clock
#include <util/delay.h>

volatile int interrupt_counter;					// Initialize global interrupt counter

ISR (INT0_vect)
{
	cli();										// Disables interrupt
	if (interrupt_counter == 40)				// If 2nd falling edge, turn on PC1 (10th bit)
	{
		PORTC |= ( 1 << PORTC1 );				// Enable PC1 ==> 10th bit
	}
	if (interrupt_counter == 80)				// If second 2nd falling edge, turn off PC1 (10th bit)
	{
		PORTC &= ~( 1 << PORTC1 );				// Disable PC1 ==> 10th bit
		interrupt_counter = 0;					// Reset interrupt_counter
	}
	sei();										// Enables interrupt
}

int main(void)
{
	sei();										// Enable interrupts
	DDRB = 0xFF;								// Enable PORTB for output 0xFF = 255 = 11111111
	DDRC = 0x03; 								// Enable PORTC for output 0x03 = 3 = 00000011
	EICRA |= ( 1 << ISC01 );					// Interrupt request on Falling Edge of INT0
	EIMSK |= ( 1 << INT0 );						// Enable external interrupt
	int counter = 0;							// Counter for digits and outputting digit
	int tenth_cycle_counter = 0;				// Counter for every ten cycles
	PORTB = counter;							// initialize PORTB = 0
	while(1)
	{
		_delay_ms(250);							// Delay function for Rising Edge trigger
		counter++;								// Increment digit count
		interrupt_counter++;					// Increment the global interrupt counter
		PORTB = counter;						// Output digit to PORTB (LED bar will display value in binary)
		tenth_cycle_counter++;					// Increment the tenth_cycle_counter
		if (tenth_cycle_counter == 10)			// Check if tenth_cycle_counter is 10, if so then it is tenth cycle
		{
			PORTC |= ( 1 << PORTC0 );			// Enable PC0, PC0 will be connected to PD2 for INT0 ==> 9th bit
		}
		if (tenth_cycle_counter == 20)			// Disable PC0 if it is the 2nd tenth cycle
		{
			PORTC &= ~( 1 << PORTC0 );			// Disable PC0,  PC0 will be connected to PD2 for INT0
			tenth_cycle_counter = 0;			// Reset tenth_cycle_counter
		}
		_delay_ms(250);							// Delay function for Falling Edge trigger
	}
}
